﻿Ext.namespace("GridParkInfo");

GridParkInfo.List=function(){
	this.txtParkName = new Ext.form.TextField({
		width:100
	});
	this.txtParkAddress = new Ext.form.TextField({
		width:150
	});

	this.cmbParkRegion = new Ext.form.ComboBox({
		width :120,
		store : new Ext.data.JsonStore({
		   url:'../gridareas/listAllRegions.form',
		   autoLoad : true,
		   root:"regions",
		   fields:["areaId","name"]
		}),
		valueField : 'areaId',
		displayField :'name',
		editable: false,
        mode: 'local',
//		typeAhead :true,
		triggerAction :'all',
		emptyText :'请选择所属区域',
		selectOnFocus :true	
	});
	
     this.btnSearch = new Ext.Toolbar.Button({
        iconCls: 'icon-search',
        text: '查询',
        tooltip: {text: '查询基地园区信息'},
        handler: this.onSearch,
        scope: this
    });
	 this.btnAdd = new Ext.Toolbar.Button({
     	text:'增加园区', 
     	scope:this, 
     	iconCls:'icon-add', 
     	handler:this.onAdd
    });
    this.btnModify = new Ext.Toolbar.Button({
    	text:'修改园区', 
    	scope:this, 
    	iconCls:'icon-modify', 
    	handler:this.onModify
    });
    this.btnDelete = new Ext.Toolbar.Button({
    	text:'删除',
    	id :'deletePark',
//    	disabled:true,
    	scope:this, 
    	iconCls:'icon-delete', 
    	handler:this.onDelete
    });
    this.store = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({
			url:'../gridparkinfo/list.form?timesnap=' + Date.parse(new Date()),
			method: 'POST'
		}),
		reader: new Ext.data.JsonReader({
			root: 'result',
			totalProperty: 'total',
			id: 'parkId',           
			fields: ['PARK_ID','NAME','GRID_NAME', 'UNIT_ID', 'PARK_NAME', 'PARK_TYPE', 'BUILDING_NUM', 'TOTAL_DOOR_NUM', 'OCCUPANCY_RATE','ATTRACT_INVESTMENT', 'MERCHANT_UNIT_PRICE', 'GARAGE_NUM', 'PROPERTY_COMPANY', 'FIRST_INDUSTRY','FIRST_INDUSTRY_PER', 'SECOND_INDUSTRY', 'SECOND_INDUSTRY_PER', 'THIRD_INDUSTRY', 'THIRD_INDUSTRY_PER','GRID_ID', 'UNIT_MANAGER', 'ADDRESS_ID', 'ADDRESS_DETAIL', 'GIS_ID','CUST_ID','CUST_NAME']
		}),
		listeners:{
			 beforeload:{
				fn:function(thiz,options){
					Ext.apply(thiz.baseParams,{
						name :this.txtParkName.getValue(),
						address :this.txtParkAddress.getValue(),
						regionId :this.cmbParkRegion.getValue()
					});
				},
				scope:this
			}
		}
	});
	this.cm = new Ext.grid.ColumnModel({
		defaults:{
			width:40,
			sortable:true,
			align:'center'
		},
		columns:[
			new Ext.grid.RowNumberer(),
			{id:'parkId',dataIndex:'PARK_ID',hidden:true},
			{id:'unitId',dataIndex:'UNIT_ID',hidden:true},
			{header:'园区名称',dataIndex:'PARK_NAME'},
			{header:'所属网格',dataIndex:'GRID_NAME',width:80},
			{header:'所属区域',dataIndex:'NAME',width:80},
			{header:'园区类型',dataIndex:'PARK_TYPE',renderer:this.renderParkType},
			{header:'园区地址',dataIndex:'ADDRESS_DETAIL'},
			{header:'入住率',dataIndex:'OCCUPANCY_RATE'},
			{header:'商铺单价',dataIndex:'MERCHANT_UNIT_PRICE'},
			{header:'物业公司名称',dataIndex:'CUST_NAME'}
		]
	});
	
	
	this.grid = new Ext.grid.GridPanel({
		title: '基地园区列表',
		region: 'center',
		store:this.store,
		height:455,
		cm:this.cm,
		loadMask: {msg: '加载中，请稍候...'},
		tbar: ['基地园区名称:',this.txtParkName,'基地园区地址:',this.txtParkAddress,/*'所属区域:',this.cmbParkRegion,*/this.btnSearch, '->',this.btnAdd,this.btnModify,this.btnDelete],
		sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
		loadMask: {msg:'加载中，请稍候...'},
		bbar:new Ext.PagingToolbar({
			pageSize:15,
			store: this.store,
			displayInfo: true,
			displayMsg: '显示第 {0} 条到 {1} 条社区信息，一共 {2} 条',
			emptyMsg: '当前没有园区记录'
		}),
		viewConfig:{forceFit:true},
		listeners:{
	      	    rowdblclick : function( grid, rowIndex,e){
	      	    	var data = grid.store.getAt(rowIndex).data;
	      	    	if(GridParkInfo.Detail.instance==null){
		      	    	GridParkInfo.Detail.instance =new GridParkInfo.Detail();
	      	    	}
					//gisEnable();//打开gis窗体
					GridParkInfo.Detail.instance.initEdit(data);
					GridParkInfo.Detail.instance.show();
				},
				scope:this				
	       }
	});
	GridParkInfo.List.superclass.constructor.call(this,{
		resizable: false,
	    frame:false,	
		items:[this.grid]
	});
	//this.on('dblclick',this.onModify,this);
	this.store.load();
};

Ext.extend(GridParkInfo.List,Ext.Viewport,{
	onSearch:function(){
		this.store.load({
			params:{
				start :0,
				limit :15,
				name :this.txtParkName.getValue(),
				address :this.txtParkAddress.getValue(),
				regionId :this.cmbParkRegion.getValue()
			}
		});
	},
	renderParkType : function(value, attr, record) {
		if(value !=null&&value !=''){
			return OMS.GRID_NC_TYPE[value][1];
		}
		return '';
	},
	onAdd:function(){
		if(GridParkInfo.Detail.instance==null){
      	    GridParkInfo.Detail.instance =new GridParkInfo.Detail();
  	    }
		//gisEnable();//打开gis窗体
		GridParkInfo.Detail.instance.initAdd();
		GridParkInfo.Detail.instance.show();
	},
	onModify:function(){
		var record = this.grid.getSelectionModel().getSelected();
		if(record==null){
			Utils.warn('请选择一条记录！','警告');
			return;
		}
		var data=record.data;
		if(GridParkInfo.Detail.instance==null){
      	    GridParkInfo.Detail.instance =new GridParkInfo.Detail();
  	    }
		//gisEnable();//打开gis窗体
		GridParkInfo.Detail.instance.initEdit(data);
		GridParkInfo.Detail.instance.show();
	},
	onDelete:function(){
		var record = this.grid.getSelectionModel().getSelected();
		if(record==null){
			Utils.warn('请选择一条记录！','警告');
			return;
		}
		Ext.MessageBox.confirm("提示","确认删除  '"+record.data.PARK_NAME+"' ？",function(btn){
            if(btn=="yes"){
				Ext.Ajax.request({
					url: '../gridparkinfo/delete.form?timesnap=' + Date.parse(new Date()),
					method: 'POST',
					scope:this,
					params: {
						park_id: record.data.PARK_ID,
						gis_id : record.data.GIS_ID,
						address_id : record.data.ADDRESS_ID,
						unit_id : record.data.UNIT_ID,
						property_company : record.data.PROPERTY_COMPANY
					},
					success:function(resp){	
						this.store.reload();
					},
					failure:function(resp){	
						Utils.info('操作失败!','系统提示');
						return;
					}
				});
			}else{}
		},this);
	}
});

function gisEnable(){
	var connSyncSession = new Ext.data.Connection({
			url : '../security/getSessionUser.form?timesnap=' + Date.parse(new Date()),
			method : 'POST'
		});
		Utils.request(connSyncSession, {
			success : function(resp, action) {
				var name = (resp.user)?resp.user.userId:'';
				if(name=="admin"){
					//Ext.getCmp("gisForm").disabled = false;
					Ext.getCmp("gisForm").enable();

				}
			}
		});
}

Ext.onReady(function(){
	Ext.QuickTips.init();
	Ext.form.Field.prototype.msgTarget='side';
	Ext.BLANK_IMAGE_URL='../resources/images/default/s.gif';
	GridParkInfo.List.instance=new GridParkInfo.List();
	GridParkInfo.List.instance.show();
	GridParkInfo.Detail.instance =new GridParkInfo.Detail();
//	var connSyncSession = new Ext.data.Connection({
//		url : '../security/getSessionUser.form?timesnap=' + Date.parse(new Date()),
//		method : 'POST'
//	});
//	Utils.request(connSyncSession, {
//		success : function(resp, action) {
//			var name = (resp.user)?resp.user.userId:'';
//			if(name=="admin"){
//				Ext.getCmp("deletePark").enable();
//				//Ext.getCmp("ParkGrid").render();
//			}	
//		}
//	});
});
